/*
 * @(#)DmsQueryRecordParser.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.dms.core;

import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import com.dcivision.dms.bean.DmsDefaultProfileSetting;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.SysUserDefinedIndexDetail;
import com.dcivision.framework.xml.XMLUtility;

/**
DmsQueryRecordParser.java

This class is the serializable bean reflecting business logic uses.

/**
 * <p>Class Name:       DmsQueryRecordParser.java    </p>
 * <p>Description:      This class mainly parse the search criteria into xml format</p>
 * @author              Jenny Li
 * @company             DCIVision Limited
 * @creation date       02/09/2004
 * @version             $Revision: 1.10.26.1 $
 */

public class DmsQueryRecordParser extends XMLUtility {

  public static final String REVISION = "$Revision: 1.10.26.1 $";


  /**
   *  Constructor - Creates a new instance of DmsQueryRecordParser
   */
  public DmsQueryRecordParser() {}

  public DmsQueryRecordParser(String file) {
    super(file);
  }
  List rootList = null;
  SessionContainer ctx=null;
  Connection conn = null;

  private String[] searchFolder = null;

  private String documentName = null;
  private String documentNameOption = "false";
  private String documentNameExactMatch = "false";
  
  private String documentType = null;
  private String documentTypeOption = "false";

  private String referenceNo = null;
  private String referenceNoOption = "false";
  private String referenceNoExactMatch = "false";

  private String description = null;
  private String descriptionOption = "false";
  private String descriptionExactMatch = "false";
  
  private String creator = null;
  private String creatorOption = "false";

  private String newDescription = null;
  private String newUserDefinedID = null;

  private String effectiveStartDate = null;
  private String effectiveEndDate = null;
  private String effectiveDateOption = "false";

  private String queryPriority = null;
  private String priorityOption = "false";

  private String createDate = null;
  private String createToDate = null;
  private String updateDate = null;
  private String updateToDate = null;
  private String createDateOption = "false";
  private String updateDateOption = "false";

  private String[] userDef = null;

  private String[] userDefTo = null;

  private String[] userDefDataType = null;

  private String[] userDefOption =null;

  private String[] userDefExactMatch = null;

  private String userDefinedFieldID = "";
  private String userDefinedFieldName=null;
  private String userDefinedFieldOption = "false";
  private String[] udfDetail_fieldType=null;
  private String[] udfDetail_fieldOption=null;
  private String[] udfDetail_value=null;
  private String[] udfDetail_value_to=null;
  private String[] udfDetail_exactMatch=null;

  /** used for tmp UDF Detail */
  private String tmpUDFOption = "false";
  private String tmpUDFExactMatch = "false";

  public void setRootList (List rootList) {
    this.rootList = rootList;
  }

  public List getRootList () {
    return (this.rootList);
  }

  public void setSessionContainer (SessionContainer ctx) {
    this.ctx = ctx;
  }

  public SessionContainer getSessionContainer () {
    return (this.ctx);
  }

  public void setConnection (Connection conn) {
    this.conn = conn;
  }

  public Connection getConnection () {
    return (this.conn);
  }

  public void setSearchFolder(String[] searchFolder) {
    this.searchFolder = searchFolder;
  }

  public String[] getSearchFolder() {
    return searchFolder;
  }

  public void setDocumentName(String documentName) {
    this.documentName = documentName;
  }

  public String getDocumentName() {
    return documentName;
  }

  public void setDocumentNameOption(String documentNameOption) {
    this.documentNameOption = documentNameOption;
  }

  public String getDocumentNameOption() {
    return documentNameOption;
  }

  public void setDocumentNameExactMatch(String documentNameExactMatch) {
    this.documentNameExactMatch = documentNameExactMatch;
  }

  public String getDocumentNameExactMatch() {
    return this.documentNameExactMatch;
  }
  
  public void setDocumentType(String documentType){
    this.documentType=documentType; 
  }
  
  public String getDocumentType(){
    return this.documentType; 
  }
  
  public void setDocumentTypeOption(String documentTypeOption){
    this.documentTypeOption=documentTypeOption; 
  }
  
  public String getDocumentTypeOption(){
    return this.documentTypeOption; 
  }

  public void setCreateDate(String createDate){
    this.createDate = createDate;
  }

  public String getCreateDate(){
    return this.createDate;
  }

  public void setUpdateDate(String updateDate){
    this.updateDate = updateDate;
  }

  public String getUpdateDate(){
    return this.updateDate;
  }

  public void setCreateToDate(String createToDate){
    this.createToDate = createToDate;
  }

  public String getCreateToDate(){
    return this.createToDate;
  }

  public void setUpdateToDate(String updateToDate){
    this.updateToDate = updateToDate;
  }

  public String getUpdateToDate(){
    return this.updateToDate;
  }

  public void setCreateDateOption(String createDateOption){
    this.createDateOption = createDateOption;
  }

  public String getCreateDateOption(){
    return this.createDateOption;
  }

  public void setUpdateDateOption(String updateDateOption){
    this.updateDateOption = updateDateOption;
  }

  public String getUpdateDateOption(){
    return this.updateDateOption;
  }

  public void setReferenceNo(String referenceNo) {
    this.referenceNo = referenceNo;
  }

  public String getReferenceNo() {
    return referenceNo;
  }

  public void setReferenceNoOption(String referenceNoOption) {
    this.referenceNoOption = referenceNoOption;
  }

  public String getReferenceNoOption() {
    return referenceNoOption;
  }

  public void setReferenceNoExactMatch(String referenceNoExactMatch) {
    this.referenceNoExactMatch = referenceNoExactMatch;
  }

  public String getReferenceNoExactMatch() {
    return this.referenceNoExactMatch;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getDescription() {
    return description;
  }

  public void setDescriptionOption(String descriptionOption) {
    this.descriptionOption = descriptionOption;
  }

  public String getDescriptionOption() {
    return descriptionOption;
  }

  public void setDescriptionExactMatch(String descriptionExactMatch) {
    this.descriptionExactMatch = descriptionExactMatch;
  }

  public String getDescriptionExactMatch() {
    return descriptionExactMatch;
  }
  
  public void setCreator(String creator) {
    this.creator = creator;
  }

  public String getCreator() {
    return creator;
  }

  public void setCreatorOption(String creatorOption) {
    this.creatorOption = creatorOption;
  }

  public String getCreatorOption() {
    return creatorOption;
  }

  public void setNewDescription(String description) {
    this.newDescription = description;
  }

  public String getNewDescription() {
    return newDescription;
  }

  public void setNewUserDefinedID(String userDefinedID) {
    this.newUserDefinedID = userDefinedID;
  }

  public String getNewUserDefinedID() {
    return newUserDefinedID;
  }

  public String getEffectiveStartDate() {
    return (this.effectiveStartDate);
  }

  public void setEffectiveStartDate(String effectiveStartDate) {
    this.effectiveStartDate = effectiveStartDate;
  }

  public String getEffectiveEndDate() {
    return (this.effectiveEndDate);
  }

  public void setEffectiveEndDate(String effectiveEndDate) {
    this.effectiveEndDate = effectiveEndDate;
  }

  public void setEffectiveDateOption(String effectiveDateOption) {
    this.effectiveDateOption = effectiveDateOption;
  }

  public String getEffectiveDateOption() {
    return this.effectiveDateOption;
  }

  public String getQueryPriority() {
    return (this.queryPriority);
  }

  public void setQueryPriority(String queryPriority) {
    this.queryPriority = queryPriority;
  }

  public String getPriorityOption() {
    return (this.priorityOption);
  }

  public void setPriorityOption(String priorityOption) {
    this.priorityOption = priorityOption;
  }

  public String[] getUserDef() {
    return (this.userDef);
  }

  public void setUserDef(String[] userDef) {
    this.userDef = userDef;
  }

  public String[] getUserDefOption() {
    return (this.userDefOption);
  }

  public void setUserDefOption(String[] userDefOption) {
    this.userDefOption = userDefOption;
  }

  public String[] getUserDefExactMatch() {
    return (this.userDefExactMatch);
  }

  public void setUserDefExactMatch(String[] userDefExactMatch) {
    this.userDefExactMatch = userDefExactMatch;
  }

  public String[] getUserDefTo() {
    return (this.userDefTo);
  }

  public void setUserDefTo(String[] userDefTo) {
    this.userDefTo = userDefTo;
  }

  public String[] getUserDefDataType() {
    return (this.userDefDataType);
  }

  public void setUserDefDataType(String[] userDefDataType) {
    this.userDefDataType = userDefDataType;
  }

  //search user defined field
  public void setUserDefinedFieldID(String userDefinedFieldID) {
    this.userDefinedFieldID = userDefinedFieldID;
  }

  public String getUserDefinedFieldID() {
    return userDefinedFieldID;
  }

  public void setUserDefinedFieldName(String userDefinedFieldName) {
    this.userDefinedFieldName = userDefinedFieldName;
  }

  public String getUserDefinedFieldName() {
    return userDefinedFieldName;
  }

  public void setUserDefinedFieldOption(String userDefinedFieldOption) {
    this.userDefinedFieldOption = userDefinedFieldOption;
  }

  public String getUserDefinedFieldOption() {
    return this.userDefinedFieldOption;
  }

  public void setUdfDetail_fieldType(String[] udfDetail_fieldType){
    this.udfDetail_fieldType=udfDetail_fieldType;
  }

  public String[] getUdfDetail_fieldType(){
    return this.udfDetail_fieldType;
  }

  public void setUdfDetail_fieldOption(String[] udfDetail_fieldOption){
    this.udfDetail_fieldOption=udfDetail_fieldOption;
  }

  public String[] getUdfDetail_fieldOption(){
    return this.udfDetail_fieldOption;
  }

  public void setUdfDetail_value(String[] udfDetail_value){
    this.udfDetail_value=udfDetail_value;
  }

  public String[] getUdfDetail_value(){
    return this.udfDetail_value;
  }

  public void setudfDetail_value_to(String[] udfDetail_value_to){
    this.udfDetail_value_to=udfDetail_value_to;
  }

  public String[] getUdfDetail_value_to(){
    return this.udfDetail_value_to;
  }

  public void setUdfDetail_exactMatch(String[] udfDetail_exactMatch){
    this.udfDetail_exactMatch=udfDetail_exactMatch;
  }

  public String[] getUdfDetail_exactMatch(){
    return this.udfDetail_exactMatch;
  }
  
  /**
   * Parses the search criteria into xml format
   * 
   * @param inputSource (org.xml.sax.InputSource)
   * @return 
   * @throws IOException
   * @throws SAXException
   * @see com.dcivision.framework.xml.XMLUtility
   */
  public void parse(InputSource inputSource) throws IOException, SAXException {
    try{
      ContentHandler handler = this.getContentHandler();
      DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(ctx, conn);
      handler.startDocument();

      //write root tag
      AttributesImpl rootElementAtt = new AttributesImpl();
      rootElementAtt.addAttribute("","","xmlns","","http://tempuri.org/data_file.xsd");
      rootElementAtt.addAttribute("","","name", "","query_name");
      this.writeStartTag("search_template", "", rootElementAtt);

      //indention
      this.setIndent("\n\t");

      this.writeStartTag("query","",new AttributesImpl());

      //indention
      this.setIndent("\n\t\t");

      //start node root_list
      this.writeStartTag("root_list","",new AttributesImpl());

      //indention
      this.setIndent("\n\t\t\t");

      for(int i=0;i<rootList.size();i++){
        DmsRoot dmsRoot=(DmsRoot)rootList.get(i);
        AttributesImpl fileElementAtt=new AttributesImpl();
        fileElementAtt.addAttribute("","","id","",dmsRoot.getID().toString());
        String rootValue=this.getRootValue(dmsRoot.getID());
        fileElementAtt.addAttribute("","","selected","",rootValue);
        this.writeTag("root","",fileElementAtt);
      }
      this.setIndent("\n\t\t");
      this.writeEndTag("root_list");
      //end of node root_list
      
      //start common
      this.writeStartTag("common","",new AttributesImpl());

      //write name tag
      AttributesImpl nameElementAtt=new AttributesImpl();
      String nameOrValue=null;
      nameElementAtt.addAttribute("","","or","",this.getDocumentNameOption());
      nameElementAtt.addAttribute("","","exact_match","",this.getDocumentNameExactMatch());
      this.setIndent("\n\t\t\t");
      this.writeTag("name",this.getDocumentName(),nameElementAtt);
      
      //write document type tag
      nameElementAtt.setValue(0,this.getDocumentTypeOption());
      nameElementAtt.setValue(1,"false");
      this.writeTag("document_type",this.getDocumentType(),nameElementAtt);
      
      //write effective date tag
      nameElementAtt.setValue(0,this.getEffectiveDateOption());
      nameElementAtt.setValue(1,"false");
      this.writeStartTag("effective_date","",nameElementAtt);
      //indention
      this.setIndent("\n\t\t\t\t");
      this.writeTag("from",this.effectiveStartDate,new AttributesImpl());
      this.writeTag("to",this.effectiveEndDate,new AttributesImpl());
      //indention
      this.setIndent("\n\t\t\t");
      this.writeEndTag("effective_date");

      //write create_date tag
      nameElementAtt.setValue(0,this.getCreateDateOption());
      nameElementAtt.setValue(1,"false");
      this.writeStartTag("create_date","value",nameElementAtt);
      // indention
      this.setIndent("\n\t\t\t\t");
      this.writeTag("from",this.createDate,new AttributesImpl());
      this.writeTag("to",this.createToDate,new AttributesImpl());
      //indention
      this.setIndent("\n\t\t\t");
      this.writeEndTag("create_date");

      //write update_date tag
      nameElementAtt.setValue(0,this.getUpdateDateOption());
      nameElementAtt.setValue(1,"false");
      this.writeStartTag("update_date","",nameElementAtt);
      // indention
      this.setIndent("\n\t\t\t\t");
      this.writeTag("from",this.getUpdateDate(),new AttributesImpl());
      this.writeTag("to",this.getUpdateToDate(),new AttributesImpl());
      //indention
      this.setIndent("\n\t\t\t");
      this.writeEndTag("update_date");
      
      //write priority tag
      nameElementAtt.setValue(0,this.getPriorityOption());
      nameElementAtt.setValue(1,"false");
      this.writeTag("priority",this.getQueryPriority(),nameElementAtt);
      
      //write reference_no tag
      nameElementAtt.setValue(0,this.getReferenceNoOption());
      nameElementAtt.setValue(1,this.getReferenceNoExactMatch());
      this.writeTag("reference_no",this.getReferenceNo(),nameElementAtt);

      //write description tag
      nameElementAtt.setValue(0,this.getDescriptionOption());
      nameElementAtt.setValue(1,this.getDescriptionExactMatch());
      this.writeTag("description",this.getDescription(),nameElementAtt);
      
      //write creator tag
      nameElementAtt.setValue(0,this.getCreatorOption());
      nameElementAtt.setValue(1,"false");
      this.writeTag("creator",this.getCreator(),nameElementAtt);
      
      this.setIndent("\n\t\t");
      this.writeEndTag("common");
      //end common

      //start user defined field
      this.writeStartTag("user_defined_field","",new AttributesImpl());
      int totalFieldCount = Integer.parseInt(com.dcivision.framework.SystemParameterFactory.getSystemParameter(com.dcivision.framework.SystemParameterConstant.DMS_DEFAULT_PROFILE_FIELD_COUNT));
      this.setIndent("\n\t\t\t");
      for(int i=0;i<totalFieldCount;i++){
        AttributesImpl udfElementAtt=new AttributesImpl();
        udfElementAtt.addAttribute("","","or","",this.getUserDefOption()[i]);
        udfElementAtt.addAttribute("","","exact_match","",this.getUserDefExactMatch()[i]);
        udfElementAtt.addAttribute("","","data_type","",this.getUserDefDataType()[i]);
        //if(DmsDefaultProfileSetting.FIELD_TYPE_DATE.equals(this.getUserDefDataType())){
          int m=i+1;
          this.writeStartTag("user_def_"+m,"",udfElementAtt);
          this.setIndent("\n\t\t\t\t");
          this.writeTag("from",this.getUserDef()[i],new AttributesImpl());
          if(!DmsDefaultProfileSetting.FIELD_TYPE_DATE.equals(this.getUserDefDataType()[i])){
            this.getUserDefTo()[i]=this.getUserDef()[i];
          }else{
            if(Utility.isEmpty(this.getUserDefTo()[i])){
              this.getUserDefTo()[i]=""; 
            }
          }
          this.writeTag("to",this.getUserDefTo()[i],new AttributesImpl());
          this.setIndent("\n\t\t\t");
          this.writeEndTag("user_def_"+m);
        /*}else{
          this.writeTag("user_def_"+i+1,this.getUserDef()[i],udfElementAtt);
        }*/
      }

//    indention
      this.setIndent("\n\t\t");

      this.writeEndTag("user_defined_field");
      //end user defined field

      //start document profile
      AttributesImpl profileElementAtt=new AttributesImpl();
      if(!Utility.isEmpty(this.getUserDefinedFieldID())){
      profileElementAtt.addAttribute("","","id","",this.getUserDefinedFieldID());
      profileElementAtt.addAttribute("","","name","",this.getUserDefinedFieldName());
      }
      this.writeStartTag("document_profile","",profileElementAtt);

      //    indention
      this.setIndent("\n\t\t\t");

      List udfDetailList = docRetrievalManager.getUDFDetailList(TextUtility.parseIntegerObj(this.getUserDefinedFieldID()));
      for(int i=0;i<udfDetailList.size();i++){
        SysUserDefinedIndexDetail sysUDFIndexDetail=(SysUserDefinedIndexDetail)udfDetailList.get(i);
        AttributesImpl fieldElementAtt=new AttributesImpl();
        fieldElementAtt.addAttribute("","","id","",sysUDFIndexDetail.getID().toString());
        fieldElementAtt.addAttribute("","","name","",sysUDFIndexDetail.getFieldName());
        fieldElementAtt.addAttribute("","","or","",this.getUdfDetail_fieldOption()[i]);
        if(Utility.isEmpty(this.getUdfDetail_exactMatch()[i]) || this.getUdfDetail_exactMatch()[i]=="null"){
          udfDetail_exactMatch[i]="false";
        }
        fieldElementAtt.addAttribute("","","exact_match","",udfDetail_exactMatch[i]);
        fieldElementAtt.addAttribute("","","data_type","",this.getUdfDetail_fieldType()[i]);
        //if("D".equals(this.getUdfDetail_fieldType()) || "N".equals(this.getUdfDetail_fieldType())){
          this.writeStartTag("field","",fieldElementAtt);
          this.setIndent("\n\t\t\t\t");
          this.writeTag("from",this.getUdfDetail_value()[i],new AttributesImpl());
          this.writeTag("to",this.getUdfDetail_value_to()[i],new AttributesImpl());
          this.setIndent("\n\t\t\t");
          this.writeEndTag("field");
        /*}else{
          this.writeTag("field",this.getUdfDetail_value()[i],fieldElementAtt);
        }*/
      }
//    indention
      this.setIndent("\n\t\t");

      this.writeEndTag("document_profile");
      //end document profile

      //    indention
      this.setIndent("\n\t");

      this.writeEndTag("query");

      //indention
      this.setIndent("\n");
      
      this.writeEndTag("search_template");
      
      //end the document
      handler.endDocument();
    }catch(Exception e){log.error(e, e);}
  }
  
  /**
   * Checks the root is in the root folder search criteria
   *
   * @param rootID root id
   * @return "true" if the rootID pass is in the root folder search criteria
   */
  protected String getRootValue(Integer rootID){
    String rootValue="false";
    for(int i=0;i<this.getSearchFolder().length;i++){
      if(this.getSearchFolder()[i]!=null){
        if(rootID.toString().equals(this.getSearchFolder()[i].toString())) {
          rootValue="true"; 
        }
      }
      if("true".equals(rootValue)){
        return rootValue; 
      }
    }
    return rootValue;
  }
}
